package com.example.handler;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

import java.util.logging.LogRecord;

public class Handler_UI extends AppCompatActivity {

    private int i=0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler);
        EditText handler_UI_EditText = (EditText) findViewById(R.id.handler_UI_EditText);

        @SuppressLint("HandlerLeak") Handler myHandler = new Handler() {
            public void handleMessage(Message msg) {
                if (msg.what == 0x123) {
                    handler_UI_EditText.setText(i+"");
                }
            }
        };
        //使用定时器,每隔200毫秒让handler发送一个空信息
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                myHandler.sendEmptyMessage(0x123);
                i++;
            }
        }, 0, 1000);
    }

}